package com.zbkj.crmeb.system.controller;

import java.util.Arrays;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.SmsConstants;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.taobao.pac.sdk.cp.dataobject.request.MODUAN_ORDER_CREATE.request;
import com.zbkj.crmeb.enums.BusinessCodeEnum;
import com.zbkj.crmeb.enums.NoticeStatusEnum;
import com.zbkj.crmeb.enums.SwitchEnum;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.system.model.MessageConfig;
import com.zbkj.crmeb.system.model.Notice;
import com.zbkj.crmeb.system.request.MessageConfigSearchRequest;
import com.zbkj.crmeb.system.request.NoticeSearchRequest;
import com.zbkj.crmeb.system.response.MessageConfigResponse;
import com.zbkj.crmeb.system.response.NoticeResponse;
import com.zbkj.crmeb.system.service.MessageConfigService;



/**
 * 通知表
 *
 * @author lss
 * @email didiok@foxmail.com
 * @date 2024-07-25 13:36:21
 */
@Slf4j
@RestController
@RequestMapping("api/admin/system/messageconfig")
public class MessageConfigController extends AdminBaseController {
    @Autowired
    private MessageConfigService messageConfigService;

    /**
     * 分页显示
     *
     * @param request          搜索条件
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MessageConfigResponse>> list(@Validated MessageConfigSearchRequest request, HttpServletRequest servletRequest) {
        log.info("list：{}", JSON.toJSONString(request));
        request.setMerId(getMerId(servletRequest));
        PageInfo<MessageConfigResponse> pageInfo = messageConfigService.getList(request);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 查询通知表信息
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    public CommonResult<MessageConfig> info(@PathVariable Integer id) {
        log.info("info id：{}", id);
        MessageConfig messageConfig = messageConfigService.getById(id);
        return CommonResult.success(messageConfig);
    }

    /**
     * 新增
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增")
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> addOrUpdate(@RequestBody @Validated MessageConfig messageConfig, HttpServletRequest servletRequest) {
        log.info("save messageConfig：{}", JSON.toJSONString(messageConfig));
        Integer merId = getMerId(servletRequest);
        messageConfig.setMerId(merId);

        MessageConfig config = messageConfigService.lambdaQuery().eq(MessageConfig::getMerId, merId)
                .eq(MessageConfig::getBusinessCode, messageConfig.getBusinessCode())
                .eq(MessageConfig::getIsDel, SwitchEnum.CLOSE.getCode())
                .one();

        if(config != null) {
            messageConfig.setId(config.getId());
        }
        return messageConfigService.addOrUpdate(messageConfig) ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 新增
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "发送信息")
    @PostMapping("/sendMessage")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> sendMessage(@RequestBody @Validated MessageConfig messageConfig, HttpServletRequest servletRequest) {
        log.info("save messageConfig：{}", JSON.toJSONString(messageConfig));

        Integer merId = messageConfig.getMerId();
        if(null == merId){
            merId = getMerId(servletRequest);
        }
        Integer businessCode = messageConfig.getBusinessCode();
        if(null == businessCode){
            businessCode = BusinessCodeEnum.MERCHANT_ACCOUNT_WARN.getCode();
        }
        Integer templateId = messageConfig.getTemplateId();
        if(null == templateId){
            templateId = SmsConstants.SMS_WARNING_TEMPLATE_ID;
        }
        String message = messageConfig.getMessage();
        if(null == message){
            message = SmsConstants.SMS_TEST_CONTENT;
        }
        return messageConfigService.sendMessage(merId, businessCode, templateId, message) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {
        log.info("delete id：{}", id);
        return messageConfigService.removeById(id) ? CommonResult.success() : CommonResult.failed();
    }

}
